package com.nova.view;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.nova.BaseActivity;
import com.nova.source.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

/**
 * ʱ��ѡ���
 */
public class TimePickerDialog implements OnTimeChangedListener
{
	private TimePicker timePicker;
	private AlertDialog dialog;

	private String time;
	private BaseActivity activity;

	public TimePickerDialog(BaseActivity activity)
	{
		super();
		this.activity = activity;
	}

	public AlertDialog showTimePicker(final TextView tv, final String sb)
	{
		View view = activity.getLayoutInflater().inflate(
				R.layout.dialog_timepicker, null);
		timePicker = (TimePicker) view.findViewById(R.id.timepicker);
		timePicker.setOnTimeChangedListener(this);

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm");
		String dd = sdf.format(calendar.getTime());
		String[] sss = dd.split(",");
		time = sss[1];

		dialog = new AlertDialog.Builder(activity).setIcon(null)
				.setTitle("选择时间").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton)
					{
//						tv.setTextColor(activity.getResources().getColor(
//								R.color.deep_green));

//						sb.append(time);
//						tv.setText(sb.toString());
						tv.setText(time);
					}
				}).setNegativeButton("取消", null).create();
		dialog.show();

		return dialog;
	}

	public AlertDialog showTimePicker(final TextView tv, final StringBuffer sb,
			final int count)
	{
		View view = activity.getLayoutInflater().inflate(
				R.layout.dialog_timepicker, null);
		timePicker = (TimePicker) view.findViewById(R.id.timepicker);
		timePicker.setOnTimeChangedListener(this);

		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm");
		String dd = sdf.format(calendar.getTime());
		String[] sss = dd.split(",");
		time = sss[1];

		dialog = new AlertDialog.Builder(activity).setIcon(null)
				.setTitle("选择时间").setView(view)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton)
					{
						tv.setTextColor(activity.getResources().getColor(
								R.color.deep_green));

						if (count == 0)
							sb.append(time);
						else
							sb.append("," + time);
						
						tv.setText(sb.toString());
					}
				}).setNegativeButton("取消", null).create();
		dialog.show();

		return dialog;
	}

	@Override
	public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
				calendar.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd,HH:mm");
		String dd = sdf.format(calendar.getTime());
		String[] sss = dd.split(",");
		time = sss[1];
	}

}
